/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <otaEthernetHeader.h>

otaEthernetHeader::otaEthernetHeader()
: m_valid(false)
{
	
}

otaEthernetHeader::otaEthernetHeader( std::string sourceMAC, std::string destinationMAC, unsigned short type )
{
	if( sourceMAC.length() != 6 || sourceMAC.length() != 6 )
	{
		m_valid = false;
	}
	else
	{
		m_sourceMAC = sourceMAC;
		m_destinationMAC = destinationMAC;
		m_type = type;
	}
}

bool otaEthernetHeader::deserialize( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset )
{
	bool ok;
	if( dataSize - offset < OTA_ETHERNET_HEADER_LENGTH )
	{
		ok = false;
	}
	else
	{
		data += offset;
		newOffset = offset;
		
		m_sourceMAC.assign( data + offset, OTA_MAC_ADDRESS_LENGTH );
		newOffset += OTA_MAC_ADDRESS_LENGTH;
		
		
		m_destinationMAC.assign( (data + offset + OTA_MAC_ADDRESS_LENGTH) , OTA_MAC_ADDRESS_LENGTH );
		newOffset += OTA_MAC_ADDRESS_LENGTH;
				
		ok = otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_type );
	}	
	return ok;
}
	
bool otaEthernetHeader::serialize( char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset )
{
	if(m_valid && (dataSize - offset >= OTA_ETHERNET_HEADER_LENGTH) )
	{
		data += offset;
		
		memcpy( data, m_sourceMAC.c_str(), OTA_MAC_ADDRESS_LENGTH );
		data += OTA_MAC_ADDRESS_LENGTH;
		
		memcpy( data, m_destinationMAC.c_str(), OTA_MAC_ADDRESS_LENGTH );
		data += OTA_MAC_ADDRESS_LENGTH;
		
		*data = m_type;
		newOffset = offset + OTA_ETHERNET_HEADER_LENGTH;
		
		return true;
	}
	else
	{
		return false;
	}
}

std::string otaEthernetHeader::getSourceMAC()
{
	return m_sourceMAC;
}
	
std::string otaEthernetHeader::getDestinationMAC()
{
	return m_destinationMAC;
}
	
unsigned short otaEthernetHeader::getType()
{
	return m_type;
}

otaEthernetHeader::~otaEthernetHeader()
{
}


